package com.vshop.platform.system.crud.dao;

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vshop.framework.core.constant.Const;
import com.vshop.platform.system.domain.entity.SysPost;
import com.vshop.platform.system.crud.mapper.SysPostMapper;
import com.vshop.platform.system.domain.param.SysPostParam;
import org.springframework.stereotype.Repository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import static com.mybatisflex.core.query.QueryMethods.findInSet;
import static com.mybatisflex.core.query.QueryMethods.number;
import static com.vshop.platform.system.domain.entity.table.SysDeptTableDef.SysDeptTable;
import static com.vshop.platform.system.domain.entity.table.SysPostTableDef.SysPostTable;
import static com.vshop.platform.system.domain.entity.table.SysUserPostTableDef.SysUserPostTable;


/**
 * 岗位信息表 服务层实现。
 *
 * @author vshop
 * @since 2024-11-18
 */
@RequiredArgsConstructor
@Slf4j
@Repository
public class SysPostDao extends ServiceImpl<SysPostMapper, SysPost> {
    public QueryWrapper buildQueryWrapper(SysPostParam param) {

        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(SysPostTable.PostId, SysPostTable.PostCode, SysPostTable.PostName,
                        SysPostTable.PostCategory, SysPostTable.SortNum, SysPostTable.Status,
                        SysPostTable.CreateTime, SysPostTable.Remark, SysPostTable.DeptId,
                        SysDeptTable.DeptName)
                .from(SysPostTable.as("p"))
                .leftJoin(SysDeptTable).as("d").on(SysDeptTable.DeptId.eq(SysPostTable.DeptId))
                .and(SysPostTable.PostCode.like(param.getPostCode()))
                .and(SysPostTable.PostCategory.like(param.getPostCategory()))
                .and(SysPostTable.PostName.like(param.getPostName()))
                .and(SysPostTable.Status.eq(param.getStatus()))
                .orderBy(SysPostTable.SortNum.asc());

        if (ObjectUtil.isNotNull(param.getDeptId())) {
            QueryWrapper inQueryWrapper = QueryWrapper.create();
            inQueryWrapper.select(SysDeptTable.DeptId)
                    .from(SysDeptTable)
                    .where(findInSet(number(param.getDeptId()), SysDeptTable.Ancestors).gt(0));
            queryWrapper.and(SysPostTable.DeptId.eq(param.getDeptId())
                    .or(SysPostTable.DeptId.in(inQueryWrapper)));
        }
        return queryWrapper;
    }

    public boolean checkExistPostName(SysPostParam param) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(SysPostTable.PostName)
                .from(SysPostTable)
                .and(SysPostTable.PostName.eq(param.getPostName()))
                .and(SysPostTable.PostId.ne(param.getPostId()));
        return this.exists(queryWrapper);
    }

    public boolean checkExistPostCode(SysPostParam param) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(SysPostTable.PostCode)
                .from(SysPostTable)
                .and(SysPostTable.PostCode.eq(param.getPostCode()))
                .and(SysPostTable.PostId.ne(param.getPostId()));
        return this.exists(queryWrapper);
    }

    /**
     * 检验岗位是否存在用户
     *
     * @param postId 岗位ID
     */
    public boolean checkPostHasUserByPostId(Long postId) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .select(SysUserPostTable.UserId)
                .from(SysUserPostTable)
                .and(SysUserPostTable.PostId.eq(postId));
        return this.count(queryWrapper) > 0;
    }

    public QueryWrapper buildQueryWrapperByDeptId(Long deptId) {
        return QueryWrapper.create()
                .select(SysPostTable.PostId, SysPostTable.PostName, SysPostTable.PostCode)
                .from(SysPostTable)
                .where(SysPostTable.Status.eq(Const.StatusNormal))
                .and(SysPostTable.DeptId.eq(deptId));
    }

}
